package com.rerun.common.config;

import com.alibaba.druid.util.DruidPasswordCallback;
import com.rerun.common.util.AES;
import org.apache.commons.lang3.StringUtils;

import java.util.Properties;

/**
 * Created with IntelliJ IDEA.
 * Author: Dax
 * Description:
 * Date: 2017/9/23
 * Time: 10:33
 */


public class DatabaseConfig extends DruidPasswordCallback {

    private static final String JDBC_PASSWORD_KEY = "jdbc.password";
    private static final long serialVersionUID = -43913067337170633L;

    @Override
    public void setProperties(Properties properties) {
        super.setProperties(properties);

        String code = properties.getProperty(JDBC_PASSWORD_KEY);


        if (StringUtils.isNotEmpty(code)) {
            try {
                String cipher = AES.decryptAES(code);
                this.setPassword(cipher.toCharArray());

            } catch (Exception e) {
                e.printStackTrace();

                this.setPassword(properties.getProperty(JDBC_PASSWORD_KEY).toCharArray());
            }
        }
    }
}
